import {getCurrentInstance,computed} from "vue";
import {useStore} from "vuex";
import {LocationQueryRaw} from "vue-router";
import {RouterName} from "@/enums";

interface routerOptions {
    path?: string;
    query?: LocationQueryRaw;
}

export default function () {
    const {proxy} = getCurrentInstance();
    const store = useStore();
    const token = computed(() => store.getters.token);
    //判断登录状态
    function checkStatus(status?: boolean) {
        if (!token.value) {
            if (!status !== false)
                (proxy as any).$message({
                    message:"请先登录",
                    type:"warning",
                });
            return false;
        }
        return true;
    }
    //导航索引
    function changeIndex(value:any) {
        proxy.$store.commit("setActiveNavName",value);
    }
    //路由管理
    function routerManager(routerName:string | number,options:routerOptions) {
        switch (routerName) {
            case RouterName.SignIn:
            case RouterName.SignUp:
            case RouterName.SignOut:
            default:
                proxy.$router.push({path:options.path});
                break;
        }
    }
    return {
        changeIndex,
        checkStatus,
        routerManager,

    };
}
